package com.runda.partypolicy.app.page.activity;


import android.widget.FrameLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.runda.partypolicy.R;
import com.runda.partypolicy.app.base.BaseFragmentActivity;
import com.runda.partypolicy.app.page.fragment.home.Fragment_Main_AddressBook;
import com.runda.partypolicy.app.page.fragment.home.Fragment_Main_Message;
import com.runda.partypolicy.app.page.fragment.home.Fragment_Main_Mine;
import com.runda.partypolicy.app.page.fragment.home.Fragment_Main_Office;
import com.runda.partypolicy.app.page.fragment.home.Fragment_Main_Task;
import com.runda.partypolicy.app.page.viewmodel.ViewModel_Main;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProviders;
import butterknife.BindView;

public class Activity_Main extends BaseFragmentActivity<ViewModel_Main> {

    @BindView(R.id.bottomBar_mainPage)
    BottomNavigationBar bottomBar;
    @BindView(R.id.frameLayout_mainPage_contentSpace)
    FrameLayout frameLayoutMainPageContentSpace;
    private Fragment currentFragment = new Fragment();
    Fragment_Main_Message mainMessage;
    Fragment_Main_AddressBook mainAddressBook;
    Fragment_Main_Task mainTask;
    Fragment_Main_Office mainOffice;
    Fragment_Main_Mine mainMine;

    @Override

    public int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    public ViewModel_Main initViewModel() {
        return ViewModelProviders.of(this, getViewModelFactory()).get(ViewModel_Main.class);
    }

    @Override
    public void setupFragments() {
//        Fragment_Main_Message firstFrgment = findFragment(Fragment_Main_Message.class);
//        if (firstFrgment == null) {
//            fragments[0] = Fragment_Main_Message.newInstance();
//            fragments[1] = Fragment_Main_AddressBook.newInstance();
//            fragments[2] = Fragment_Main_Task.newInstance();
//            fragments[3] = Fragment_Main_Office.newInstance();
//            fragments[4] = Fragment_Main_Mine.newInstance();
//            loadMultipleRootFragment(R.id.frameLayout_mainPage_contentSpace, 0,
//                    fragments[0], fragments[1], fragments[2], fragments[3],fragments[4]);
//        } else {
//            fragments[0] = firstFrgment;
//            fragments[1] = findFragment(Fragment_Main_AddressBook.class);
//            fragments[2] = findFragment(Fragment_Main_Task.class);
//            fragments[3] = findFragment(Fragment_Main_Office.class);
//            fragments[4] = findFragment(Fragment_Main_Mine.class);
//
//        }

        mainMessage = Fragment_Main_Message.newInstance();
        mainAddressBook = Fragment_Main_AddressBook.newInstance();
        mainTask = Fragment_Main_Task.newInstance();
        mainOffice = Fragment_Main_Office.newInstance();
        mainMine = Fragment_Main_Mine.newInstance();
        currentFragment=mainMessage;


    }

    @Override
    public void initEvents() {
        setupBottomBar();
    }

    private void setupBottomBar() {
        bottomBar
                .addItem(new BottomNavigationItem(R.drawable.ic_bottom_msg_active, R.string.mainMessage)
                        .setInactiveIconResource(R.drawable.ic_bottom_msg))
                .addItem(new BottomNavigationItem(R.drawable.ic_bottom_addrss_active, R.string.mainAddressList)
                        .setInactiveIconResource(R.drawable.ic_bottom_address))
                .addItem(new BottomNavigationItem(R.drawable.ic_bottom_task_active, R.string.mainTask)
                        .setInactiveIconResource(R.drawable.ic_bottom_task))
                .addItem(new BottomNavigationItem(R.drawable.ic_bottom_office_active, R.string.mainOffice)
                        .setInactiveIconResource(R.drawable.ic_bottom_office)).
                addItem(new BottomNavigationItem(R.drawable.ic_bottom_mine_active, R.string.mainMine)
                        .setInactiveIconResource(R.drawable.ic_bottom_mine));
        bottomBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                switch (position){
                    case 0:
                        showHideFragment(mainMessage).commit();
                        break;
                    case 1:
                        showHideFragment(mainAddressBook).commit();
                        break;
                    case 2:
                        showHideFragment(mainTask).commit();
                        break;
                    case 3:
                        showHideFragment(mainOffice).commit();
                        break;
                    case 4:
                        showHideFragment(mainMine).commit();
                        break;
                }

            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {

            }
        });
        bottomBar.setFirstSelectedPosition(0);
        bottomBar.initialise();
    }


    private FragmentTransaction showHideFragment(Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (!fragment.isAdded()) {
            //第一次使用switchFragment()时currentFragment为null，所以要判断一下
            if (fragment != null) {
                fragmentTransaction.hide(fragment);
            }
            fragmentTransaction.add(R.id.frameLayout_mainPage_contentSpace, fragment, fragment.getClass().getName());
        } else {
            fragmentTransaction.hide(currentFragment);
            fragmentTransaction.show(fragment);
        }
        currentFragment = fragment;
        return fragmentTransaction;
    }

    @Override
    public void start() {

    }

    @Override
    public void initNotSignEvent() {

    }

    @Override
    public void initNoNetworkEvent() {

    }

    @Override
    public void initShowMessageEvent() {

    }

    @Override
    public void initTokenOverTimeEvent() {

    }

    @Override
    public void initShowOrDismissWaitingEvent() {

    }

    @Override
    public void initStateLayoutEvent() {

    }


}
